﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using System.Web.Security;
using ThuySan.Bussiness;

namespace ThuySan
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("Custom1", // Route name
                           "{category}/{url}/{id}.html", // URL with parameters
                           new
                           {
                               controller = "HomeProduct",
                               action = "Detail",
                               id = UrlParameter.Optional,
                               url = UrlParameter.Optional,
                               category = UrlParameter.Optional 
                           } // Parameter defaults
                   );
            routes.MapRoute("Custom2", // Route name
                            "{url}/{categoryId}.html", // URL with parameters
                            new
                            {
                                controller = "Home",
                                action = "BrowseCategory",
                                categoryId = UrlParameter.Optional,
                                url = UrlParameter.Optional
                            } // Parameter defaults
                    );
            routes.MapRoute("Type", // Route cho trang phan loai
                           "phan-loai.html", // URL with parameters
                           new
                           {
                               controller = "Home",
                               action = "TypeTagList"                               
                           } 
                   );
            routes.MapRoute("Custom3", // Route name
                           "{tag}-{tagId}-1.html", // URL with parameters
                           new
                           {
                               controller = "Home",
                               action = "BrowseTag",
                               tagId = UrlParameter.Optional,
                               tag = UrlParameter.Optional
                           } // Parameter defaults
                   );
            
           
            routes.MapRoute("Contact", // Route cho trang contact
                           "contact.html", // URL with parameters
                           new
                           {
                               controller = "Home",
                               action = "Contact"
                           }
                   );
            routes.MapRoute("Newroducts", // Route cho trang contact
                           "new-products.html", // URL with parameters
                           new
                           {
                               controller = "Home",
                               action = "NewProducts"
                           }
                   );
            routes.MapRoute("ReducedPrice", // Route cho trang contact
                         "reduced-price.html", // URL with parameters
                         new
                         {
                             controller = "Home",
                             action = "ReducedPriced"
                         }
                 );
            routes.MapRoute("Search", // Route cho trang contact
                         "search.html", // URL with parameters
                         new
                         {
                             controller = "Home",
                             action = "Search",
                             q = UrlParameter.Optional,
                             type = UrlParameter.Optional,
                             otype = UrlParameter.Optional,
                             n = UrlParameter.Optional,
                             page = UrlParameter.Optional
                         }
                 );
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            Database.SetInitializer<FishesStoreContext>(null);

            //ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
        }

        protected void Session_Start()
        {
            if (User.Identity.IsAuthenticated)
            {
                FormsAuthentication.SetAuthCookie(User.Identity.Name, true);
            }
        }

    }
}